package com.reform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reform.mapper.ViewBatchMapper;
import com.reform.pojo.phase.*;
import com.reform.pojo.project.PhaseTime;
import com.reform.pojo.project.ProjectClassification;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.utils.RedisUtils;
import com.reform.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-08-06
 */
@Service
public class ViewBatchServiceImpl extends ServiceImpl<ViewBatchMapper, ViewBatch> implements IViewBatchService {

    @Autowired
    ViewBatchMapper viewBatchMapper;
    @Autowired
    IProjectClassificationService projectClassificationService;
    @Autowired
    IBatchService batchService;
    @Autowired
    IPhaseTimeService phaseTimeService;
    @Autowired
    INumProjectService numProjectService;
    @Autowired
    IViewProjectService viewProjectService;
    @Autowired
    IViewEmpowerBatchService viewEmpowerBatchService;
    @Autowired
    IExpertService expertService;
    @Autowired
    IProjectService projectService;
    @Autowired
    IBatchPhaseService batchPhaseService;
    @Autowired
    private RedisUtils redisUtils;

    @Value("${jwt.expireTime}")
    Long expireTime;

    @Override
    public Result getAllBatchInfo() {
        return Result.success(ResultCode.SUCCESS, viewBatchMapper.selectList(null));
    }

    @Override
    public ViewBatch getLatelyBatchInfo() {
        List<ViewBatch> viewBatchList = viewBatchMapper.selectList(new QueryWrapper<ViewBatch>().orderByDesc("phase_teacher_end"));
        return viewBatchList.get(0);
    }

    @Override
    public Result creatBatch(BatchInfo batchInfo) throws SQLException {
        String batchId = UUIDUtils.getUUID();

        Batch batch = new Batch(batchId, batchInfo.getBatchName(), batchInfo.getCompany(), batchInfo.getLevelId(),
                batchInfo.getMaxHost(), batchInfo.getMaxParticipants(), batchInfo.getMiddleTime(), batchInfo.getPostTime());

        PhaseTime phaseTime = new PhaseTime(batchId, batchId, batchInfo.getBatchName(),
                batchInfo.getPhaseTeacherStart(), batchInfo.getPhaseTeacherEnd(), batchInfo.getPhaseDepartmentStart(),
                batchInfo.getPhaseDepartmentEnd(), batchInfo.getPhaseSchoolFirstStart(), batchInfo.getPhaseSchoolFirstEnd(),
                batchInfo.getPhaseExpertStart(), batchInfo.getPhaseExpertEnd(), batchInfo.getPhaseSchoolSecondStart(),
                batchInfo.getPhaseSchoolSecondEnd(), batchInfo.getType(), batchInfo.getStatus());

        ProjectClassification projectClassification = new ProjectClassification(null, batchId, batchInfo.getClassifyName());
        int res_batch = batchService.creatBatch(batch);
        int res_phase_time = phaseTimeService.creatPhaseTime(phaseTime);
        int res_classify_name = projectClassificationService.creatProjectClassification(projectClassification);
        if (res_batch > 0 && res_classify_name > 0 && res_phase_time > 0) {
            numProjectService.initNumProject(batchId);
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result updateBatch(BatchInfo batchInfo) throws SQLException {

        Batch batch = new Batch(batchInfo.getBatchId(), batchInfo.getBatchName(), batchInfo.getCompany(), batchInfo.getLevelId(), batchInfo.getMaxHost(), batchInfo.getMaxParticipants(), batchInfo.getMiddleTime(), batchInfo.getPostTime());

        PhaseTime phaseTime = new PhaseTime(batchInfo.getPhaseId(), batchInfo.getBatchId(), batchInfo.getBatchName(),
                batchInfo.getPhaseTeacherStart(), batchInfo.getPhaseTeacherEnd(), batchInfo.getPhaseDepartmentStart(),
                batchInfo.getPhaseDepartmentEnd(), batchInfo.getPhaseSchoolFirstStart(), batchInfo.getPhaseSchoolFirstEnd(),
                batchInfo.getPhaseExpertStart(), batchInfo.getPhaseExpertEnd(), batchInfo.getPhaseSchoolSecondStart(),
                batchInfo.getPhaseSchoolSecondEnd(), batchInfo.getType(), batchInfo.getStatus());

        ProjectClassification projectClassification = new ProjectClassification(batchInfo.getClassifyId(), batchInfo.getBatchId(), batchInfo.getClassifyName());

        int res_batch = batchService.updateBatch(batch);
        int res_phase_time = phaseTimeService.updatePhaseTime(phaseTime);
        int res_classify_name = projectClassificationService.updateProjectClassification(projectClassification);

        if (res_batch > 0 && res_classify_name > 0 && res_phase_time > 0) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result deleteBatch(String batchId) throws SQLException {
        int res_batch = batchService.deleteBatch(batchId);
        int res_phase_time = phaseTimeService.deletePhaseTime(batchId);
        int res_classify_name = projectClassificationService.deleteProjectClassification(batchId);

        if (res_batch > 0 || res_classify_name > 0 || res_phase_time > 0) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result selectMaxHost(String batch_id) throws SQLException {
        return viewProjectService.selectMaxHost(batch_id);
    }

    @Override
    public Result selectMaxParticipants(String batch_id) {
        return viewProjectService.selectMaxParticipants(batch_id);
    }

    @Override
    public List<String> getBatchId() {
        List<ViewBatch> batch_id = viewBatchMapper.selectList(null);
        List<String> res = new ArrayList<>();
        for (ViewBatch viewBatch : batch_id) {
            res.add(viewBatch.getBatchId());
        }
        return res;
    }

    @Override
    public Result selectClassificationByBatchId(String batch_id) {
        return viewProjectService.selectClassificationByBatchId(batch_id);
    }

    @Override
    public Result getBatchByDepId(int depId) {
        return viewEmpowerBatchService.getBatchByDepId(depId);
    }

    @Override
    public Result getAllBatchById(String batchId) {
        return Result.success(ResultCode.SUCCESS, viewBatchMapper.selectList(new QueryWrapper<ViewBatch>().eq("batch_id", batchId)));
    }

    @Override
    public Result getBatchIdByGH(String gh) throws SQLException {
        List<String> batchIdList = expertService.getBatchIdByGH(gh, 1);
        List<ViewBatch> viewBatchList = new ArrayList<>();
        for (String batchId : batchIdList) {
            viewBatchList.add(viewBatchMapper.selectOne(new QueryWrapper<ViewBatch>().eq("batch_id", batchId)));
        }
        return Result.success(ResultCode.SUCCESS, viewBatchList);
    }

    @Override
    public List<String> getBatchIdByPhaseId(String phaseId) {
        return viewBatchMapper.getBatchIdByPhaseId(phaseId);
    }

    @Override
    public Result getBatchName() {
        List<BatchName> resList = viewBatchMapper.getBatchName();
        return Result.success(ResultCode.SUCCESS, resList);
    }

    @Override
    public Result getBatchInfoIncludeDelay(Integer batchStatus) {
        /*
         * 1 查询所有项目状态为 22 的项目的批次编号 => batchIdList
         * 2 查看 batchIdList 中的批次参加的阶段
         * 3 若已结项，返回该批次信息
         */

        // 1 查询所有项目状态为 22 的项目的批次编号 => batchIdList
        /*
        SELECT
	    project.batch_id
        FROM
	    project
        WHERE
	    project.pro_approval = 22
         */
        // 2 查看 batchIdList 中的批次参加的阶段 phaseIdList
        /*
        SELECT phase_id,batch_id FROM batch_phase WHERE batch_id in (batchPhaseList)
         */
        // 3 若已结项，返回该批次信息
        /*
        SELECT phase_id FROM phase_time WHERE phase_id = batchPhaseList.getPhaseId() and status = 12
        if(phase_id != null){
            return batchPhaseList.getBatchId();
        }
         */
        int approval = 22;
        List<String> batchIdList = projectService.getBatchIdByApproval(approval);
        if (batchIdList == null) {
            return Result.success(ResultCode.SUCCESS);
        }
        List<BatchPhase> batchPhaseList = batchPhaseService.getBatchPhaseByBatchIdList(batchIdList);
        int status = 12;
        List<BatchInfoIncludeDelay> batchInfoIncludeDelayList = new ArrayList<>();
        for (BatchPhase batchPhase : batchPhaseList) {
            String phaseId = phaseTimeService.getPhaseIdByStatus(batchPhase.getPhaseId(), status);
            if (phaseId != null) {
                batchInfoIncludeDelayList.add(viewBatchMapper.getBatchInfoIncludeDelay(batchPhase.getBatchId(), batchStatus));
            }
        }
        return Result.success(ResultCode.SUCCESS, batchInfoIncludeDelayList);
    }
}
